using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using  DeloitteForms = Deloitte.Windows;
using Deloitte.Data;
using Deloitte.Windows.Controls;

namespace Deloitte.Windows.Forms
{
	/// <summary>
	/// Summary description for FormDataGrid.
	/// </summary>
	public class DataGridForm : System.Windows.Forms.Form
	{
		#region "Control Declarations"
		private DeloitteForms.Controls.DataViewSortControl  ctlDV=null;
		private DeloitteForms.Controls.DataViewRowFilterControl ctlDVFilter=null;
		private DeloitteForms.Controls.ExcelExportControl ctlXL=null;
		private DeloitteForms.Controls.DistinctColumnValuesControl ctlSelectDistinct=null;
#endregion						
		private System.Windows.Forms.DataGrid dataGrid1;
		private DataTable2 myDataTable;
		private DataView myView;
        private Control ctlOptions;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem miSort;
		private System.Windows.Forms.MenuItem miOptions;
		private System.Windows.Forms.MenuItem miOptionsClear;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem miExcelExport;
		private System.Windows.Forms.MenuItem miFilter;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem miViewCurrentRows;
		private System.Windows.Forms.MenuItem miViewDeletedRows;
		private System.Windows.Forms.ToolTip toolTip1;
		private System.Windows.Forms.MenuItem miModifiedCurrRows;
		private System.Windows.Forms.MenuItem miModifiedOriginalRows;
		private System.Windows.Forms.MenuItem miOriginalRows;
		private System.Windows.Forms.MenuItem miUnchangedRows;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.MenuItem mdiHideShowCols;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.Panel panelOptions;
        private System.Windows.Forms.MenuItem miSelectDistinct;
        private BackgroundWorker backgroundWorker1;
        private BindingSource hCSISFRAMETPROMPTBindingSource;
        private Button btnCancel;
        private Button btnOK;        
		private System.ComponentModel.IContainer components;

		public DataGridForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
	
			this.CommonConstructorCode();
			
		}
		
		public DataGridForm(DataTable2 aDataTable, bool isDGReadOnly)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			try
			{
				this.CommonConstructorCode();
				this.CurrentDataTable = aDataTable;				
				this.dataGrid1.ReadOnly=isDGReadOnly;			
			}
			catch (Exception ex){throw new Exception(ex.ToString());}
		}
		public DataGridForm(DataTable aDataTable, bool isDGReadOnly)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
		
			try
			{
				this.CommonConstructorCode();
				this.CurrentDataTable = new DataTable2(aDataTable);				
				this.dataGrid1.ReadOnly=isDGReadOnly;			
			}
			catch (Exception ex){throw new Exception(ex.ToString());}
		}
		public DataTable2 CurrentDataTable
		{
			get{return this.myDataTable;}
			set
			{
				this.myDataTable = value;
				this.myView = this.myDataTable.DefaultView;
				this.dataGrid1.DataSource = this.myView;
			}
		}
		private void CommonConstructorCode()
		{
			this.SetDataGridEvents();							
			this.panelOptions.ControlAdded +=new ControlEventHandler(panelOptions_ControlAdded);
			this.panelOptions.ControlRemoved +=new ControlEventHandler(panelOptions_ControlRemoved);					
		}
		private void SetOptionsPanel()
		{	
			this.panelOptions.Controls.Clear();			
			this.panelOptions.Controls.Add(ctlOptions);
			this.ctlOptions.Size = this.panelOptions.Size;
			this.ctlOptions.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));		
		}
		private void SetDataGridEvents()
		{
			try
			{							
				dataGrid1.DataSourceChanged += new EventHandler(DataGrid1_DataSourceChanged);
				this.Resize += new EventHandler(FormDataGrid_Resize);
			}
			catch{}
		}
		private void DataGrid1_DataSourceChanged(object sender, EventArgs e)
		{
			try
			{
				if(this.dataGrid1.DataSource != null)
				{
					DataGrid thisGrid = (DataGrid) sender;
					if(this.dataGrid1.DataSource.GetType().Name == "DataView")
					{
						this.myView = (DataView) this.dataGrid1.DataSource;
						//Utility.AutoFormatDataGridTableStyle(myView.Table, this.dataGrid1, this.dataGrid1.ReadOnly, false);
					}
					else if(this.dataGrid1.DataSource.GetType().Name == "DataTable")
					{
						this.myView = ((DataTable) this.dataGrid1.DataSource).DefaultView;
						//Utility.AutoFormatDataGridTableStyle(myView.Table, this.dataGrid1, this.dataGrid1.ReadOnly, false);
					}
				}
			}
			catch (Exception ex){throw new Exception(ex.ToString());}
		}

		public bool isDataGridReadOnly
		{
			get{return this.dataGrid1.ReadOnly;}
			set{this.dataGrid1.ReadOnly = value;}
		}
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            this.dataGrid1 = new System.Windows.Forms.DataGrid();
            this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
            this.miOptions = new System.Windows.Forms.MenuItem();
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.miExcelExport = new System.Windows.Forms.MenuItem();
            this.miFilter = new System.Windows.Forms.MenuItem();
            this.miSort = new System.Windows.Forms.MenuItem();
            this.miSelectDistinct = new System.Windows.Forms.MenuItem();
            this.miOptionsClear = new System.Windows.Forms.MenuItem();
            this.menuItem2 = new System.Windows.Forms.MenuItem();
            this.menuItem3 = new System.Windows.Forms.MenuItem();
            this.miViewCurrentRows = new System.Windows.Forms.MenuItem();
            this.miViewDeletedRows = new System.Windows.Forms.MenuItem();
            this.miModifiedCurrRows = new System.Windows.Forms.MenuItem();
            this.miModifiedOriginalRows = new System.Windows.Forms.MenuItem();
            this.miOriginalRows = new System.Windows.Forms.MenuItem();
            this.miUnchangedRows = new System.Windows.Forms.MenuItem();
            this.menuItem4 = new System.Windows.Forms.MenuItem();
            this.mdiHideShowCols = new System.Windows.Forms.MenuItem();
            this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
            this.panel1 = new System.Windows.Forms.Panel();
            this.panelOptions = new System.Windows.Forms.Panel();
            this.splitter1 = new System.Windows.Forms.Splitter();
            this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
            this.hCSISFRAMETPROMPTBindingSource = new System.Windows.Forms.BindingSource(this.components);
            this.btnCancel = new System.Windows.Forms.Button();
            this.btnOK = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
            this.panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.hCSISFRAMETPROMPTBindingSource)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGrid1
            // 
            this.dataGrid1.AlternatingBackColor = System.Drawing.Color.Lavender;
            this.dataGrid1.BackColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.BackgroundColor = System.Drawing.Color.LightGray;
            this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dataGrid1.CaptionBackColor = System.Drawing.Color.LightSteelBlue;
            this.dataGrid1.CaptionForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.DataMember = "";
            this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Top;
            this.dataGrid1.FlatMode = true;
            this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8F);
            this.dataGrid1.ForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.GridLineColor = System.Drawing.Color.Gainsboro;
            this.dataGrid1.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
            this.dataGrid1.HeaderBackColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
            this.dataGrid1.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.LinkColor = System.Drawing.Color.Teal;
            this.dataGrid1.Location = new System.Drawing.Point(0, 0);
            this.dataGrid1.Name = "dataGrid1";
            this.dataGrid1.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
            this.dataGrid1.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.SelectionBackColor = System.Drawing.Color.CadetBlue;
            this.dataGrid1.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.Size = new System.Drawing.Size(744, 448);
            this.dataGrid1.TabIndex = 0;
            // 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.miOptions,
            this.menuItem2});
            // 
            // miOptions
            // 
            this.miOptions.Index = 0;
            this.miOptions.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem1,
            this.miFilter,
            this.miSort,
            this.miSelectDistinct,
            this.miOptionsClear});
            this.miOptions.Text = "&Tools";
            // 
            // menuItem1
            // 
            this.menuItem1.Index = 0;
            this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.miExcelExport});
            this.menuItem1.Text = "&Export";
            // 
            // miExcelExport
            // 
            this.miExcelExport.Index = 0;
            this.miExcelExport.Text = "E&xcel";
            this.miExcelExport.Click += new System.EventHandler(this.miExcelExport_Click);
            // 
            // miFilter
            // 
            this.miFilter.Index = 1;
            this.miFilter.Text = "&Filter";
            this.miFilter.Click += new System.EventHandler(this.miFilter_Click);
            // 
            // miSort
            // 
            this.miSort.Index = 2;
            this.miSort.Text = "&Sort";
            this.miSort.Click += new System.EventHandler(this.miSort_Click);
            // 
            // miSelectDistinct
            // 
            this.miSelectDistinct.Index = 3;
            this.miSelectDistinct.Text = "Select &Distinct";
            this.miSelectDistinct.Click += new System.EventHandler(this.miSelectDistinct_Click);
            // 
            // miOptionsClear
            // 
            this.miOptionsClear.Index = 4;
            this.miOptionsClear.Text = "&Clear";
            this.miOptionsClear.Click += new System.EventHandler(this.miOptionsClear_Click);
            // 
            // menuItem2
            // 
            this.menuItem2.Index = 1;
            this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem3,
            this.menuItem4});
            this.menuItem2.Text = "&View";
            // 
            // menuItem3
            // 
            this.menuItem3.Index = 0;
            this.menuItem3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.miViewCurrentRows,
            this.miViewDeletedRows,
            this.miModifiedCurrRows,
            this.miModifiedOriginalRows,
            this.miOriginalRows,
            this.miUnchangedRows});
            this.menuItem3.Text = "&Rows";
            // 
            // miViewCurrentRows
            // 
            this.miViewCurrentRows.Index = 0;
            this.miViewCurrentRows.Text = "&Current Rows";
            this.miViewCurrentRows.Click += new System.EventHandler(this.miViewCurrentRows_Click);
            // 
            // miViewDeletedRows
            // 
            this.miViewDeletedRows.Index = 1;
            this.miViewDeletedRows.Text = "&Deleted Rows";
            this.miViewDeletedRows.Click += new System.EventHandler(this.miViewDeletedRows_Click);
            // 
            // miModifiedCurrRows
            // 
            this.miModifiedCurrRows.Index = 2;
            this.miModifiedCurrRows.Text = "&Modified Current Rows";
            this.miModifiedCurrRows.Click += new System.EventHandler(this.miModifiedCurrRows_Click);
            // 
            // miModifiedOriginalRows
            // 
            this.miModifiedOriginalRows.Index = 3;
            this.miModifiedOriginalRows.Text = "Mod&ified Original Rows";
            this.miModifiedOriginalRows.Click += new System.EventHandler(this.miModifiedOriginalRows_Click);
            // 
            // miOriginalRows
            // 
            this.miOriginalRows.Index = 4;
            this.miOriginalRows.Text = "&Original Rows";
            this.miOriginalRows.Click += new System.EventHandler(this.miOriginalRows_Click);
            // 
            // miUnchangedRows
            // 
            this.miUnchangedRows.Index = 5;
            this.miUnchangedRows.Text = "&Unchanged Rows";
            this.miUnchangedRows.Click += new System.EventHandler(this.miUnchangedRows_Click);
            // 
            // menuItem4
            // 
            this.menuItem4.Index = 1;
            this.menuItem4.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.mdiHideShowCols});
            this.menuItem4.Text = "&Columns";
            // 
            // mdiHideShowCols
            // 
            this.mdiHideShowCols.Index = 0;
            this.mdiHideShowCols.Text = "Hide/Show Columns";
            this.mdiHideShowCols.Click += new System.EventHandler(this.mdiHideShowCols_Click);
            // 
            // panel1
            // 
            this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.panel1.Controls.Add(this.panelOptions);
            this.panel1.Controls.Add(this.splitter1);
            this.panel1.Controls.Add(this.dataGrid1);
            this.panel1.Location = new System.Drawing.Point(16, 48);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(744, 556);
            this.panel1.TabIndex = 4;
            // 
            // panelOptions
            // 
            this.panelOptions.BackColor = System.Drawing.Color.White;
            this.panelOptions.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panelOptions.Location = new System.Drawing.Point(0, 452);
            this.panelOptions.Name = "panelOptions";
            this.panelOptions.Size = new System.Drawing.Size(744, 104);
            this.panelOptions.TabIndex = 2;
            // 
            // splitter1
            // 
            this.splitter1.BackColor = System.Drawing.SystemColors.Control;
            this.splitter1.Dock = System.Windows.Forms.DockStyle.Top;
            this.splitter1.Location = new System.Drawing.Point(0, 448);
            this.splitter1.MinExtra = 0;
            this.splitter1.MinSize = 100;
            this.splitter1.Name = "splitter1";
            this.splitter1.Size = new System.Drawing.Size(744, 4);
            this.splitter1.TabIndex = 1;
            this.splitter1.TabStop = false;
            // 
            // backgroundWorker1
            // 
            this.backgroundWorker1.WorkerSupportsCancellation = true;
            // 
            // btnCancel
            // 
            this.btnCancel.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
            this.btnCancel.Location = new System.Drawing.Point(59, 12);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(57, 21);
            this.btnCancel.TabIndex = 9;
            this.btnCancel.Text = "&Cancel";
            this.btnCancel.UseVisualStyleBackColor = true;
            // 
            // btnOK
            // 
            this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnOK.Location = new System.Drawing.Point(16, 12);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new System.Drawing.Size(37, 21);
            this.btnOK.TabIndex = 8;
            this.btnOK.Text = "&OK";
            this.btnOK.UseVisualStyleBackColor = true;
            // 
            // DataGridForm
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(768, 619);
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.btnOK);
            this.Controls.Add(this.panel1);
            this.Menu = this.mainMenu1;
            this.Name = "DataGridForm";
            this.Text = "Data Grid Form";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
            this.panel1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.hCSISFRAMETPROMPTBindingSource)).EndInit();
            this.ResumeLayout(false);

		}
		#endregion

		private void lnkOK_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
		{
			this.DialogResult= DialogResult.OK;
		}

		private void lnkCancel_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
		{
			this.DialogResult= DialogResult.Cancel;
		}

		private void FormDataGrid_Resize(object sender, EventArgs e)
		{
			if(this.ctlOptions!=null)this.ctlOptions.Size =this.panelOptions.Size;			
		}

		private void miSort_Click(object sender, System.EventArgs e)
		{
			if(this.ctlDV ==null) this.ctlDV = new DeloitteForms.Controls.DataViewSortControl(this.myView);
			this.ctlOptions  = this.ctlDV;
			this.SetOptionsPanel();
		}

		private void panelOptions_ControlAdded(object sender, ControlEventArgs e)
		{
			if(this.dataGrid1.Size.Height==this.panel1.Size.Height)this.dataGrid1.Size= new Size(this.panel1.Size.Width, (int)(this.panel1.Size.Height * .75));
		}

		private void panelOptions_ControlRemoved(object sender, ControlEventArgs e)
		{
			}

		private void miOptionsClear_Click(object sender, System.EventArgs e)
		{
			this.panelOptions.Controls.Clear();
			this.dataGrid1.Size = new Size(this.panel1.Size.Width, this.panel1.Size.Height);
		}

		private void miExcelExport_Click(object sender, System.EventArgs e)
		{
			try
			{
				if(this.ctlXL ==null) this.ctlXL = new ExcelExportControl(this.myView.Table);
				this.ctlOptions  = this.ctlXL;
				this.SetOptionsPanel();
			}
			catch(Exception ex){throw new Exception(ex.ToString());}
		}

		private void miFilter_Click(object sender, System.EventArgs e)
		{
			try
			{
				if(this.ctlDVFilter ==null) this.ctlDVFilter = new DeloitteForms.Controls.DataViewRowFilterControl(this.myView);
				this.ctlOptions  = this.ctlDVFilter;
				this.SetOptionsPanel();
			}
			catch(Exception ex){throw new Exception(ex.ToString());}
		}

		private void miViewDeletedRows_Click(object sender, System.EventArgs e)
		{
			this.myView.RowStateFilter = System.Data.DataViewRowState.Deleted;
		}

		private void miViewCurrentRows_Click(object sender, System.EventArgs e)
		{
			this.myView.RowStateFilter = System.Data.DataViewRowState.Deleted;
		}

		private void miModifiedCurrRows_Click(object sender, System.EventArgs e)
		{
			this.myView.RowStateFilter = System.Data.DataViewRowState.ModifiedCurrent;
		}

		private void miModifiedOriginalRows_Click(object sender, System.EventArgs e)
		{
			this.myView.RowStateFilter = System.Data.DataViewRowState.ModifiedOriginal;
		}

		private void miOriginalRows_Click(object sender, System.EventArgs e)
		{
			this.myView.RowStateFilter = System.Data.DataViewRowState.OriginalRows;
		}

		private void miUnchangedRows_Click(object sender, System.EventArgs e)
		{
			this.myView.RowStateFilter = System.Data.DataViewRowState.Unchanged;
		}

		private void mdiHideShowCols_Click(object sender, System.EventArgs e)
		{
			
		}

		private void miSelectDistinct_Click(object sender, System.EventArgs e)
		{
			try
			{
				if(this.ctlSelectDistinct ==null) this.ctlSelectDistinct = new DeloitteForms.Controls.DistinctColumnValuesControl(this.CurrentDataTable);
				this.ctlOptions  = this.ctlSelectDistinct;
				this.SetOptionsPanel();
			}
			catch(Exception ex){throw new Exception(ex.ToString());}
		}
     

	}
}
